package com.thiny.android.app.tnote.util;

import java.io.File;
import java.io.IOException;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;

public class SDCardUtil {

	/** get external Storage available space */
	public static long getExternaltStorageAvailableSpace() {
		if (!Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			return 0;
		}
		File path = Environment.getExternalStorageDirectory();
		StatFs statfs = new StatFs(path.getPath());
		long blockSize = statfs.getBlockSize();
		long availableBlocks = statfs.getAvailableBlocks();
		return blockSize * availableBlocks;
	}

	/** get external Storage available space */
	public static long getExternaltStorageTotalSpace() {
		File path = Environment.getExternalStorageDirectory();
		StatFs statfs = new StatFs(path.getPath());
		long blockSize = statfs.getBlockSize();
		long totalBlocks = statfs.getBlockCount();
		return blockSize * totalBlocks;
	}

	/** get sdcard2 external Storage available space */
	public static long getSdcard2StorageAvailableSpace() {
		if (!Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			return 0;
		}
		String path = getSdcard2StorageDirectory();
		File file = new File(path);
		if (!file.exists())
			return 0;
		StatFs statfs = new StatFs(path);
		long blockSize = statfs.getBlockSize();
		long availableBlocks = statfs.getAvailableBlocks();
		return blockSize * availableBlocks;
	}

	/** get sdcard2 external Storage total space */
	public static long getSdcard2StorageTotalSpace() {
		String path = getSdcard2StorageDirectory();
		File file = new File(path);
		if (!file.exists())
			return 0;
		StatFs statfs = new StatFs(path);
		long blockSize = statfs.getBlockSize();
		long totalBlocks = statfs.getAvailableBlocks();
		return blockSize * totalBlocks;
	}

	/** get EMMC internal Storage available space */
	public static long getEmmcStorageAvailableSpace() {
		String path = getEmmcStorageDirectory();
		File file = new File(path);
		if (!file.exists())
			return 0;
		StatFs statfs = new StatFs(path);
		long blockSize = statfs.getBlockSize();
		long availableBlocks = statfs.getAvailableBlocks();

		return blockSize * availableBlocks;
	}

	/** get EMMC internal Storage available space */
	public static long getEmmcStorageTotalSpace() {
		String path = getEmmcStorageDirectory();
		File file = new File(path);
		if (!file.exists())
			return 0;
		StatFs statfs = new StatFs(path);
		long blockSize = statfs.getBlockSize();
		long totalBlocks = statfs.getBlockCount();

		return blockSize * totalBlocks;
	}

	/**
	 * 获取手机内部总的存储空间
	 * 
	 * @return
	 */
	public static long getInternalStorageTotalSpace() {
		File path = Environment.getDataDirectory();
		StatFs stat = new StatFs(path.getPath());
		long blockSize = stat.getBlockSize();
		long totalBlocks = stat.getBlockCount();
		return totalBlocks * blockSize;
	}

	public final static String getExternalStorageDirectory() {
		return Environment.getExternalStorageDirectory() + File.separator + "";
	}

	public final static String getExternalStoragePublicDirectory(String type) {
		return Environment.getExternalStoragePublicDirectory(type)
				.getAbsolutePath();
	}

	public final static String getSdcard2StorageDirectory() {
		return "/mnt/sdcard2";
	}

	public final static String getEmmcStorageDirectory() {
		return "/mnt/emmc";
	}

	private static String externalStoragePrivateDirectory;

	static String getExternalPrivateFilesDirectory(Context context) {
		if (externalStoragePrivateDirectory == null)
			externalStoragePrivateDirectory = context.getExternalFilesDir(null)
					.getAbsolutePath();
		return externalStoragePrivateDirectory;
	}

	 

	public static String getFileName(String file) {
		if (file == null)
			return null;
		int index = file.lastIndexOf("/");
		return file.substring(index + 1);
	}

	public static boolean runShellScriptForWait(final String cmd)
			throws SecurityException {
		ShellThread thread = new ShellThread(cmd);
		thread.setDaemon(true);
		thread.start();
		int k = 0;
		while (!thread.isReturn() && k++ < 20) {
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (k >= 20) {
			thread.interrupt();
		}
		return thread.isSuccess();
	}

	/** 用于执行shell脚本的线程 */
	private static class ShellThread extends Thread {
		private boolean isReturn;
		private boolean isSuccess;
		private String cmd;

		public boolean isReturn() {
			return isReturn;
		}

		public boolean isSuccess() {
			return isSuccess;
		}

		/**
		 * @param cmd
		 *            shell命令内容
		 * @param isReturn
		 *            线程是否已经返回
		 * @param isSuccess
		 *            Process是否执行成功
		 */
		public ShellThread(String cmd) {
			this.cmd = cmd;
		}

		@Override
		public void run() {
			try {
				Runtime runtime = Runtime.getRuntime();
				Process proc;
				try {
					proc = runtime.exec(cmd);
					isSuccess = (proc.waitFor() == 0);
				} catch (IOException e) {
					e.printStackTrace();
				}
				isSuccess = true;
			} catch (InterruptedException e) {
			}
			isReturn = true;
		}

	}

}
